{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Summary of Basic Scheme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Thinking Recursively, Summary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Decide on the base cases\n", "2. A base case is the \"smallest\" or \"simplest\" case\n", "3. After that, assume that the function exists, and use it\n", "4. Divide the problem up into smaller pieces" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define snoc\n", " (lambda (item lst)\n", " (cond\n", " ((null? lst) (cons item '()))\n", " (else (cons (car lst) \n", " (snoc item (cdr lst)))))))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(snoc 1 '()) ;; -> (1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(c b a)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(snoc 'a '(c b)) ;; -> (c b a)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1 2 3 4)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(snoc '4 '(1 2 3)) ;; -> (1 2 3 4)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define rac\n", " (lambda (lst)\n", " (cond\n", " ((null? lst) (error 'rac \"can't take the rac of an empty list\"))\n", " ((null? (cdr lst)) (car lst))\n", " (else (rac (cdr lst))))))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(rac '(1 2 3 4)) ;; -> 4" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(rac '(a)) ;; -> a" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[0;31m\n", "Traceback (most recent call last):\n", " File \"In [8]\", line 1, col 1, in 'rac'\n", " File \"In [5]\", line 4, col 19, in 'error'\n", " File \"In [5]\", line 4, col 19\n", "RunTimeError: Error in 'rac': can't take the rac of an empty list\n", "\n", "\u001b[0m" ] } ], "source": [ "(rac '())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Scheme Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scheme (like Python) can take functions as arguments, and return them. This is called \"functions as first-class objects.\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define add1\n", " (lambda (number)\n", " (+ 1 number)))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2 3 4)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(map add1 '(1 2 3))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define my-map\n", " (lambda (f lst)\n", " (cond\n", " ((null? lst) '())\n", " (else (cons (f (car lst))\n", " (my-map f (cdr lst)))))))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2 3 4)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(my-map add1 '(1 2 3))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(104 106 108)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(map + '(1 2 3) '(3 4 5) '(100 100 100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Higher-Order Functions" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define compose\n", " (lambda (f g)\n", " (lambda (lst)\n", " (f (g lst)))))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define my-cadr (compose car cdr)) ;; my-cadr is a " ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(my-cadr '(1 2 3)) ;; -> 2" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2 5 8)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(map my-cadr '((1 2 3) (4 5 6) (7 8 9)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.4 What is a Programming Language?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. a computer program\n", "2. takes in source code in a language (often recursively defined)\n", "3. produces either a compiled program, or interpreter\n", "\n", "Lab03 will begin the process of implementing Scheme in Python." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(math)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(import \"math\")" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "8.0" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(math.pow 2 3)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(define expt math.pow)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "243.0" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(expt 3 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Scheme 3", "language": "scheme", "name": "calysto_scheme" }, "language_info": { "codemirror_mode": { "name": "scheme" }, "mimetype": "text/x-scheme", "name": "scheme", "pygments_lexer": "scheme" } }, "nbformat": 4, "nbformat_minor": 1 }